﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;

namespace VkSample104
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, ISceneModule
    {
        public Scene Scene { get; set; }
        [Autowired]
        private ILogger<AssetsModule> _Logger { get; set; }

        protected override async Task Load(VulkanGraphicsManager manager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var modelsPath = Path.Combine(basePath, "Assets", "Models");
                var texPath = Path.Combine(basePath, "Assets", "Textures");
                var lDevice = manager.VulkanDevice;

                manager.TryGetRenderGroup("Common", out IRenderGroup common);
                ISEADocument document = new ISEADocument();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "Test104.isea"));
                var renders = await manager.CreateRenderObjectsFromISEA<RenderObject>(document);

                var commonArchetype = ContextManager.CreateArchetype("Common", new TransformComponent());
                var lightArchetype = ContextManager.CreateArchetype(new LightComponent());
                ContextManager.CreateContexts(1, commonArchetype, common, renders[0], new Transform() { Position = new Vector3(0, -10f, 0), Scaling = Vector3.One });
                var light = manager.CreatePointLight<RenderObject>("main", new Vector3(50, 100, 50), new LightStrength(0.4f, 0.4f, 0.4f, 1.0f));
                ContextManager.CreateContexts(1, lightArchetype, common, light);
            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
